package com.tanhua.dubbo.api.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.mapper.BlackListMapper;
import com.tanhua.dubbo.mapper.QuestionMapper;
import com.tanhua.dubbo.mapper.SettingsMapper;
import com.tanhua.dubbo.pojo.BlackList;
import com.tanhua.dubbo.pojo.Question;
import com.tanhua.dubbo.pojo.Settings;
import com.tanhua.dubbo.pojo.UserInfo;
import com.tanhua.dubbo.vo.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService(version = "1.0.0")
public class MyApiImpl implements com.tanhua.dubbo.api.MyApi {

    @DubboReference(version = "1.0.0")
    private UserInfoApi userInfoApi;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private SettingsMapper settingsMapper;

    @Autowired
    private BlackListMapper blackListMapper;

    @Override
    public UserInfo queryUserInfo(Long userId) {
        UserInfo userInfo = this.userInfoApi.queryByUserId(userId);
        if (ObjectUtil.isEmpty(userInfo)) {
            return new UserInfo();
        }
        return userInfo;
    }

    @Override
    public Boolean saveQuestions(Long userId, String content) {

        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Question question = questionMapper.selectOne(queryWrapper);
        if(ObjectUtil.isEmpty(question)) {
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);

            return questionMapper.insert(question) > 0;
        }
        question.setTxt(content);
        return questionMapper.update(question, queryWrapper) > 0;

    }

    @Override
    public Settings querySettings(Long userId) {
        QueryWrapper<Settings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Settings settings = this.settingsMapper.selectOne(queryWrapper);
        return settings;
    }

    @Override
    public String queryQuestion(Long userId) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Question question = this.questionMapper.selectOne(queryWrapper);
        return question.getTxt();
    }

    @Override
    public Boolean updateSettings(Long userId,
                                  Boolean likeNotification,
                                  Boolean pinglunNotification,
                                  Boolean gonggaoNotification) {
        Settings settings = this.querySettings(userId);
        if(ObjectUtil.isEmpty(settings)) {
            return false;
        }
        settings.setLikeNotification(Convert.toInt(likeNotification));
        settings.setPinglunNotification(Convert.toInt(pinglunNotification));
        settings.setGonggaoNotification(Convert.toInt(gonggaoNotification));
        int update = this.settingsMapper.updateById(settings);
        return update > 0;
    }

    @Override
    public PageInfo<BlackList> queryBlackList(Long userId, Integer page, Integer pageSize) {
        PageInfo<BlackList> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);

        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        List<BlackList> blackUserList = this.blackListMapper.selectList(queryWrapper);
        pageInfo.setRecords(blackUserList);
        return pageInfo;
    }

    @Override
    public Integer queryBlackListCount(Long userId) {
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Integer count = this.blackListMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public Boolean deleteBlackUser(Long userId, Long blackUserId) {
        QueryWrapper<BlackList> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("black_user_id", blackUserId);
        int count = this.blackListMapper.delete(wrapper);
        return count > 0;
    }
}
